package com.zyd.blog.homeart.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zyd.blog.framework.object.PageResult;
import com.zyd.blog.homeart.service.BackstageFineArtService;
import com.zyd.blog.persistence.beans.*;
import com.zyd.blog.persistence.beans.management.Shop;
import com.zyd.blog.persistence.mapper.*;
import me.zhyd.oauth.utils.UuidUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class BackstageFineArtServiceImpl implements BackstageFineArtService {
    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Autowired
    private BackstageFineArtMapper backstageFineArtMapper;
    @Autowired
    private FineArtMapper fineArtMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private CenterMapper centerMapper;
    @Autowired
    private CommentRelationMapper commentRelationMapper;
    @Autowired
    private MyParticipateMapper myParticipateMapper;

    @Override
    public Integer insertFineArt(FineArt fineArt) {
        fineArt.setAddTime(sdf.format(System.currentTimeMillis()));
        fineArt.setFineId(UuidUtils.getUUID());
        fineArt.setZanNumber(0);
        fineArt.setDiscussNumber(0);
        fineArt.setCollectionNumber(0);
        fineArt.setExamine("0");
        fineArt.setNow("1");
        fineArt.setDraft("1");
        fineArt.setTop("1");
        fineArt.setCategory("0");
        fineArt.setUserId("8");
        fineArt.setDeleteState("1");
        fineArt.setUserName("系统");
        fineArt.setNum(0);
        fineArt.setUseState("0");
        fineArt.setUserImage("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2932779449,831692746&fm=26&gp=0.jpg");
        fineArt.setTopTime(sdf.format(System.currentTimeMillis()));
        return backstageFineArtMapper.insert(fineArt);
    }

    @Override
    public Integer editFineArt(String  fineId,String  fineTitle,String fineImage,String top,
                               String  fineContent) {

        return backstageFineArtMapper.editFineArt(fineId,fineTitle,fineImage,top,fineContent);
    }

    @Override
    public Integer deleteFineArt(String fineId) throws Exception {

        //根据文章id找到评论id  放到list
        List<CommentId> commentIds = commentMapper.commentAll(fineId);
        for (CommentId commentId : commentIds) {
            try {
                commentMapper.deleteComment(commentId.getCommentId());
            } catch (Exception e) {
                throw new Exception("删除评论失败");
            }
            try {
                commentRelationMapper.deleteCommentRelation(commentId.getCommentId());
            } catch (Exception e) {
                throw new Exception("删除comment_relation失败");
            }
            try {
                commentMapper.deleteCommentReply(commentId.getCommentId());
            } catch (Exception e) {
                throw new Exception("删除评论回复失败");
            }
        }
        try {
            centerMapper.deleteCenter(fineId);
        } catch (Exception e) {
            throw new Exception("删除center失败！");
        }
        try {
            myParticipateMapper.deleteArticle(fineId);
        } catch (Exception e) {
            throw new Exception("从我的参与中删除失败");
        }
        try {
            backstageFineArtMapper.deleteFineArt(fineId);

        } catch (Exception e) {
            throw new Exception("删除文章失败");
        }
        return 1;
    }

    @Override
    public Map<String, Object> searchDesign(String value, int page, int count) {
        Map<String,Object> map = new HashMap<>();
        map.put("page",page);
        int i = backstageFineArtMapper.count(value);
        if (i%count==0){
            map.put("allPage",i/count);
        }else {
            map.put("allPage",i/count+1);
        }
        map.put("list",backstageFineArtMapper.searchDesign(value,page*count-count,count));
        return map;
    }

    @Override
    public PageResult softBackstage(int page, int count) {
        PageResult pageResult = new PageResult();
        int i = backstageFineArtMapper.selectCount(null);
        pageResult.setTotal((long)i);
        List<FineArt> softProducts = backstageFineArtMapper.FineArtBackstage(page * count - count, count);
        pageResult.setRows(softProducts);
        return pageResult;
    }
    @Override
    public Map<String, Object> specific(String designId,String state) throws Exception {
        //state 类别0 :用户发出的
        // 1:系统发出的
        Map<String,Object> map = new HashMap<>();
        FineArt specific;
        try {
            if (state.equals("1")){
                specific = backstageFineArtMapper.get(designId);
                map.put("specific",specific);
            }else if (state.equals("0")){
                specific = backstageFineArtMapper.get(designId);
                String[] split = specific.getProductId().split(",");
                List<Product> list = new ArrayList<>();
                for (String s : split) {
                    Product all = productMapper.all(s);
                    list.add(all);
                }
                list.removeAll(Collections.singleton(null));
                if (list.size()==0){
                    map.put("shop", Collections.emptyList());

                }else {
                    map.put("shop",list);
                }
                map.put("specific",specific);
            }

        } catch (Exception e) {
            throw new Exception("获取内容失败");
        }
        return map;
    }

    @Override
    public FineArt get(String id) {
        return backstageFineArtMapper.get(id);
    }

    @Override
    public Integer updateIn(FineArt fineArt) {

        return backstageFineArtMapper.updateIn(fineArt.getFineId(),fineArt.getUseState());
    }

    @Override
    public Integer hide(String id, String commentId, String content) {
        //此篇文章  评论数量-1
        Integer integer=0;
        Integer hide=0;
        if (content.equals("隐藏")){
            fineArtMapper.commentReduce(id);

            hide = commentMapper.hide(commentId);
        }
        if (content.equals("不隐藏")){
            fineArtMapper.commentNum(id);
            integer = commentMapper.noHide(commentId);
        }
        return hide+integer;
    }
}
